import App from './App'


import Vue from 'vue'
import './uni.promisify.adaptor'
Vue.config.productionTip = false
App.mpType = 'app'

Vue.prototype.$showLoading = ()=>{
	uni.showLoading({
		mask:true
	})
}

Vue.prototype.$isLogin = ()=>{
	if(uni.getStorageSync('token')){
		return false
	}else {
		uni.redirectTo({
			url:'/pages/my/login'
		})
		return true
	}
}

Vue.prototype.$onLaunched = new Promise(resolve => {
	Vue.prototype.$isResolve = resolve
})

Vue.prototype.$baseUrl = '/api'
// Vue.prototype.$baseUrl = 'http://18237185359.gnway.cc'
Vue.prototype.$http = (url,data,method,ct)=>{
	return new Promise(resolve => {
		let form={'Content-Type' : 'application/json'}
		if(ct){
			form = {
				'Content-Type' : 'application/x-www-form-urlencoded',
			}
		}
		uni.request({
		    url: Vue.prototype.$baseUrl + url,
		    method: method || 'GET',
		    data: data || {},
		    header:{
				...form,
				Authorization : uni.getStorageSync('token')?'Bearer '+uni.getStorageSync('token'):''
			},
		    success: (res) => {
				if(res.statusCode==200){
					if(res.data.code==200||res.data.code==0){
						resolve(res.data);
					}else if(res.data.message){
						uni.showToast({
							title:res.data.message,
							icon:'none'
						})
					}
				}
		    }
		});
	})
}

Vue.prototype.$timeAgo = (dateTimeStamp,type)=>{
	if(type==1){
		var datetime1 = new Date();
		datetime1.setTime(dateTimeStamp);
		var Nyear1 = datetime1.getFullYear();
		var Nmonth1 = datetime1.getMonth() + 1 < 10 ? "0" + (datetime1.getMonth() + 1) : datetime1.getMonth() + 1;
		var Ndate1 = datetime1.getDate() < 10 ? "0" + datetime1.getDate() : datetime1.getDate();
		var Nhour1 = datetime1.getHours() < 10 ? "0" + datetime1.getHours() : datetime1.getHours();
		var Nminute1 = datetime1.getMinutes() < 10 ? "0" + datetime1.getMinutes() : datetime1.getMinutes();
		var Nsecond1 = datetime1.getSeconds() < 10 ? "0" + datetime1.getSeconds() : datetime1.getSeconds();
		var result1 = Nyear1 + "-" + Nmonth1 + "-" + Ndate1 + ' ' + Nhour1 + ':' + Nminute1 + ':' + Nsecond1
		return result1;
	}
	
	var minute = 1000 * 60;      //把分，时，天，周，半个月，一个月用毫秒表示
	var hour = minute * 60;
	var day = hour * 24;
	var week = day * 7;
	var halfamonth = day * 15;
	var month = day * 30;
	var now = new Date().getTime();   //获取当前时间毫秒
	var result = '';
	var diffValue = now - dateTimeStamp;//时间差
	   
	if (diffValue < 0) {
	    return "刚刚";
	}
	var minC = diffValue / minute;  //计算时间差的分，时，天，周，月
	var hourC = diffValue / hour;
	var dayC = diffValue / day;
	var weekC = diffValue / week;
	var monthC = diffValue / month;
	if (monthC >= 1 && monthC <= 3) {
	    result = " " + parseInt(monthC) + "月前"
	} else if (weekC >= 1 && weekC <= 3) {
	    result = " " + parseInt(weekC) + "周前"
	} else if (dayC >= 1 && dayC <= 6) {
	    result = " " + parseInt(dayC) + "天前"
	} else if (hourC >= 1 && hourC <= 24) {
	    result = " " + parseInt(hourC) + "小时前"
	} else if (minC >= 1 && minC <= 59) {
	    result = " " + parseInt(minC) + "分钟前"
	} else if (diffValue >= 0 && diffValue <= minute) {
	    result = "刚刚"
	} else {
	    var datetime = new Date();
	    datetime.setTime(dateTimeStamp);
	    var Nyear = datetime.getFullYear();
	    var Nmonth = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
	    var Ndate = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
	    var Nhour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
	    var Nminute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
	    var Nsecond = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
	    result = Nyear + "-" + Nmonth + "-" + Ndate
	}
	return result;
}

Vue.prototype.$generateUUID = ()=>{
	let d = new Date().getTime()
	const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
		const r = (d + Math.random() * 16) % 16 | 0
		d = Math.floor(d / 16)
		return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16)
	})
	return uuid
}

uni.setStorageSync('version','1.0.0')

const app = new Vue({
  ...App
})
app.$mount()

